<template>
	<view class="container" :style="{height: height}">
		<!-- 操作工具 -->
		<view class="tool-view">
			<!-- 文字相关操作 -->
			<view class="font-more" :style="{ height: showMoreTool ? '100rpx' : 0 }">
				<jinIcon class="single" type="&#xe6e7;" font-size="44rpx" title="加粗" @click="setBold"
					:color="showBold ? activeColor : '#666666'"></jinIcon>
				<jinIcon class="single" type="&#xe6fe;" font-size="44rpx" title="斜体" @click="setItalic"
					:color="showItalic ? activeColor : '#666666'"></jinIcon>
				<jinIcon class="single" type="&#xe6f8;" font-size="44rpx" title="分割线" @click="setIns"
					:color="showIns ? activeColor : '#666666'"></jinIcon>
				<jinIcon class="single" type="&#xe6e3;" font-size="44rpx" title="标题" @click="setHeader"
					:color="showHeader ? activeColor : '#666666'"></jinIcon>
				<jinIcon class="single" type="&#xe6f1;" font-size="44rpx" title="居中" @click="setCenter"
					:color="showCenter ? activeColor : '#666666'"></jinIcon>
				<jinIcon class="single" type="&#xe6ed;" font-size="44rpx" title="居右" @click="setRight"
					:color="showRight ? activeColor : '#666666'"></jinIcon>
			</view>
			<view class="setting-layer-mask" v-if="showSettingLayer" @click="showSetting"></view>
			<view class="tool">
				<jinIcon class="single" type="&#xe6f3;" font-size="44rpx" title="插入图片" @click="insertImage"></jinIcon>
				<jinIcon class="single" type="&#xe6f9;" font-size="44rpx" title="修改文字样式" @click="showMore"
					:color="showMoreTool ? activeColor : '#666666'"></jinIcon>
				<jinIcon class="single" type="&#xe6eb;" font-size="44rpx" title="分割线" @click="insertDivider"></jinIcon>
				<jinIcon class="single" type="&#xe6e8;" font-size="44rpx" title="撤销" @click="undo"></jinIcon>
				<jinIcon class="single" type="&#xe705;" font-size="44rpx" title="重做" @click="redo"></jinIcon>
			</view>
		</view>
		<editor class="ql-container" :placeholder="$t('forum.qsrnr')" :show-img-size="true" :show-img-toolbar="true"
			:show-img-resize="true" @ready="onEditorReady" id="editor" @statuschange="statuschange" @focus="editFocus"
			@blur="editBlur" v-model="htmls" ref="editot"></editor>

	</view>
</template>

<script>
	import { Upload3 } from '@/api/Ctrls.js'
	import { compressImage } from '@/utils/compressImage.js'
	import jinIcon from './jin-icons.vue';
	export default {
		props: {
			// 点击图片时显示图片大小控件
			showImgSize: {
				type: Boolean,
				default: false
			},
			// 点击图片时显示工具栏控件
			showImgToolbar: {
				type: Boolean,
				default: false
			},
			// 点击图片时显示修改尺寸控件
			showImgResize: {
				type: Boolean,
				default: false
			},
			// 占位符
			placeholder: {
				type: String,
				default: ''
			},
			// 图片上传的地址
			uploadFileUrl: {
				type: String,
				default: '#'
			},
			// 上传文件时的name
			fileKeyName: {
				type: String,
				default: 'file'
			},
			// 上传图片时，http请求的header
			header: {
				type: Object
			},
			// 初始化html
			value: {
				type: [Object, String]
			},
			// 整个控件的高度
			height: {
				type: String,
				default: '100%'
			}
		},
		computed: {

		},
		data() {
			return {
				showMoreTool: false,
				showBold: false,
				showItalic: false,
				showIns: false,
				showHeader: false,
				showCenter: false,
				showRight: false,
				showSettingLayer: false,
				activeColor: '#F56C6C',
				htmls: '',
			};
		},
		components: {
			jinIcon
		},
		watch: {
			htmls(val) {
				if (val?.detail) {
					this.$emit("input", val.detail)
				}

			}
		},
		mounted() {},
		methods: {
			onEditorReady(e) {
				uni.createSelectorQuery()
					.in(this)
					.select('.ql-container')
					.fields({
						size: true,
						context: true
					}, res => {
						this.editorCtx = res.context;
						this.editorCtx.setContents({
							html: this.value
						})
					})
					.exec();
			},
			undo() {
				this.editorCtx.undo();
			},
			// 插入图片
			insertImage() {
				let that = this;
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					async success({ tempFilePaths, tempFiles }) {
						try {
							let imageUrl;
							uni.showLoading()
							// #ifdef H5
							//H5压缩时 直接用文件列表 tempFiles[0]
							imageUrl = await compressImage(tempFiles[0])
							// #endif
							//app压缩时 需要传文件路径 tempFilePaths[0]
							// #ifdef APP-PLUS
							imageUrl = await compressImage(tempFilePaths[0])
							// #endif
							console.log(tempFilePaths[0])
							console.log(imageUrl)
							const { data, msg, success } = await Upload3(imageUrl)
							uni.hideLoading()
							if (!success) {
								return that.Toast(msg)
							}
							that.editorCtx.insertImage({
								src: data, // 此处需要将图片地址切换成服务器返回的真实图片地址
								alt: '图片',
								success: function(e) {}
							});
						} catch (e) {
							console.log(e)
							//TODO handle the exception
						}
					},
				})
			},
			/// 插入分割线
			insertDivider() {
				this.editorCtx.insertDivider();
			},
			redo() {
				this.editorCtx.redo();
			},
			showMore() {
				this.showMoreTool = !this.showMoreTool;
				this.editorCtx.setContents()
			},
			setBold() {
				this.showBold = !this.showBold;
				this.editorCtx.format('bold');
			},
			setItalic() {
				this.showItalic = !this.showItalic;
				this.editorCtx.format('italic');
			},
			checkStatus(name, detail, obj) {
				if (detail.hasOwnProperty(name)) {
					this[obj] = true;
				} else {
					this[obj] = false;
				}
			},
			statuschange(e) {
				var detail = e.detail;
				this.checkStatus('bold', detail, 'showBold');
				this.checkStatus('italic', detail, 'showItalic');
				this.checkStatus('ins', detail, 'showIns');
				this.checkStatus('header', detail, 'showHeader');
				if (detail.hasOwnProperty('align')) {
					if (detail.align == 'center') {
						this.showCenter = true;
						this.showRight = false;
					} else if (detail.align == 'right') {
						this.showCenter = false;
						this.showRight = true;
					} else {
						this.showCenter = false;
						this.showRight = false;
					}
				} else {
					this.showCenter = false;
					this.showRight = false;
				}
			},
			setIns() {
				this.showIns = !this.showIns;
				this.editorCtx.format('ins');
			},
			setHeader() {
				this.showHeader = !this.showHeader;
				this.editorCtx.format('header', this.showHeader ? 'H2' : false);
			},
			setCenter() {
				this.showCenter = !this.showCenter;
				this.editorCtx.format('align', this.showCenter ? 'center' : false);
			},
			setRight() {
				this.showRight = !this.showRight;
				this.editorCtx.format('align', this.showRight ? 'right' : false);
			},
			showSetting() {
				this.showSettingLayer = !this.showSettingLayer;
			},
			async editFocus(e) {},
			editBlur(e) {},
			release(isPublic) {
				this.showSettingLayer = false;
				return new Promise((resolve, reject) => {
					this.editorCtx.getContents({
						success: res => {
							Object.assign(res, {
								isPublic: isPublic
							})
							resolve(res)
						}
					})
				})

			},
		}
	};
</script>

<style scoped lang="scss">
	.container {
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		height: 100%;
		box-sizing: border-box;
	}

	.ql-container {
		position: relative;
		z-index: 1;
		font-size: 34rpx;
		width: 100%;
		flex: 1;
		box-sizing: border-box;
		color: #fff;
		padding: 24rpx 24rpx;
		padding-bottom: 120rpx;

		/deep/ .ql-editor {
			&::before {
				font-size: 24rpx;
				color: #999;
			}

			hr {
				border-color: #999;
			}
		}
	}

	.tool-view {
		position: fixed;
		z-index: 9;
		bottom: 0;
		left: 0;
		background: var(--bg_block);
		width: 100vw;
		border-bottom: 1rpx solid var(--border);
		/* margin-top: 20px; */
	}

	.tool {
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
		width: 100%;
	}

	.font-more {
		display: flex;
		align-items: center;
		justify-content: space-around;
		width: 100%;
		overflow: hidden;
		transition: all 0.15s;
	}

	.setting-layer {
		position: absolute;
		bottom: 100rpx;
		background: #fff;
		width: 250rpx;
		right: 20rpx;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
		border-radius: 8rpx;
	}

	.setting-layer .single {
		height: 80rpx;
		font-size: 32rpx;
		padding: 0 30rpx;
		display: flex;
		align-items: center;
		line-height: 80rpx;
		flex-direction: row;
		color: #666;
	}

	.setting-layer .single .icon {
		margin-right: 20rpx;
	}

	.setting-layer-mask {
		position: fixed;
		left: 0;
		top: 0;
		width: 100vw;
		height: 100vh;
		background: transparent;
	}
</style>